dnl -*- mode: Autoconf; -*-

dnl -----------------------------------------------------------------------------------------
dnl option for V8 support
dnl -----------------------------------------------------------------------------------------

V8_CPPFLAGS=""
V8_LDFLAGS=""
V8_LIBS=""

AC_ARG_WITH(v8,
  AS_HELP_STRING([--with-v8=DIR], [where the v8 library and includes are located]),
  [V8_CPPFLAGS="-I$withval/include"
   V8_LDFLAGS="-L$withval/lib"
   V8_LIB_PATH="$withval/lib"
   V8="$withval"]
)

AC_ARG_WITH(v8-lib,
  AS_HELP_STRING([--with-v8-lib=DIR], [where the v8 library is located]),
  [V8_LDFLAGS="-L$withval"
   V8_LIB_PATH="$withval"]
)

TR_STATIC_ENABLE([v8])

dnl -----------------------------------------------------------------------------------------
dnl save flags
dnl -----------------------------------------------------------------------------------------

SAVE_CPPFLAGS="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS $V8_CPPFLAGS"

SAVE_LDFLAGS="$LDFLAGS"
LDFLAGS="$LDFLAGS $V8_LDFLAGS"

SAVE_LIBS="$LIBS"

dnl -----------------------------------------------------------------------------------------
dnl checks for the V8 library
dnl -----------------------------------------------------------------------------------------

AC_MSG_NOTICE([--------------------------------------------------------------------------------])
AC_MSG_NOTICE([CHECKING FOR GOOGLE V8])
AC_MSG_NOTICE([--------------------------------------------------------------------------------])

AC_LANG(C++)

tr_V8="yes"

AX_CXX_CHECK_LIB([v8], [#include <v8.h>], [v8::V8::GetVersion()], [V8_LIBS="-lv8"], [tr_V8="no"])

if test "x$tr_V8" != xyes;  then
  AC_MSG_ERROR([Please install the V8 library from Google])
fi

if test "x$V8_CPPFLAGS" != x;  then
  TR_INCLUDE([V8_CPPFLAGS])
fi

dnl -----------------------------------------------------------------------------------------
dnl add substitutions
dnl -----------------------------------------------------------------------------------------

AC_SUBST(V8_CPPFLAGS)
AC_SUBST(V8_LDFLAGS)
AC_SUBST(V8_LIBS)

dnl -----------------------------------------------------------------------------------------
dnl save flags
dnl -----------------------------------------------------------------------------------------

LIBS="$LIBS ${V8_LIBS}"
LDFLAGS="$LDFLAGS ${V8_LDFLAGS}"
CPPFLAGS="$CPPFLAGS ${V8_CPPFLAGS}"

dnl -----------------------------------------------------------------------------------------
dnl grep V8 version number
dnl -----------------------------------------------------------------------------------------

if test "$cross_compiling" = yes; then :
  TRI_V8_VERSION="V8"
  AC_MSG_WARN([cannot compute V8 version number when cross compiling])
else
  if test "x`type -t ac_fn_cxx_try_run`" = "xfunction" 2> /dev/null;  then

cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h.  */
#include <v8.h>
#include <stdio.h>

int main (int, char**) {
  printf("%s\n", v8::V8::GetVersion());
}

_ACEOF

    if test "x$V8_LIB_PATH" != x;  then
      if test "$USE_DYLD" = yes; then
	save_DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH"
	DYLD_LIBRARY_PATH="$V8_LIB_PATH:$DYLD_LIBRARY_PATH"
      else
	save_LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
	DYLD_LIBRARY_PATH="$V8_LIB_PATH:$LD_LIBRARY_PATH"
      fi
    fi

    AC_MSG_CHECKING([V8 version])

    if ac_fn_cxx_try_run "$LINENO" >conftest.output;  then
      TRI_V8_VERSION=`cat conftest.output`
      AC_MSG_RESULT([$TRI_V8_VERSION])
    else
      AC_MSG_RESULT([failed])
      AC_MSG_ERROR([Please install the V8 library from Google])
    fi

    rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.beam conftest.$ac_ext conftest.output

    if test "x$V8_LIB_PATH" != x;  then
      if test "$USE_DYLD" = yes; then
	DYLD_LIBRARY_PATH="$save_DYLD_LIBRARY_PATH"
      else
	LD_LIBRARY_PATH="$save_LD_LIBRARY_PATH"
      fi
    fi
  else
    TRI_V8_VERSION="V8"
    AC_MSG_WARN([cannot compute V8 version number, old autoconf version])
  fi
fi

dnl -----------------------------------------------------------------------------------------
dnl restore flags
dnl -----------------------------------------------------------------------------------------

LIBS="$SAVE_LIBS"
LDFLAGS="$SAVE_LDFLAGS"
CPPFLAGS="$SAVE_CPPFLAGS"

V8_CPPFLAGS="${V8_CPPFLAGS} -DTRI_V8_VERSION='\"${TRI_V8_VERSION}\"'"

dnl -----------------------------------------------------------------------------------------
dnl informational output
dnl -----------------------------------------------------------------------------------------

LIB_INFO="$LIB_INFO|V8 VERSION: ${TRI_V8_VERSION}"

FLAG_INFO="$FLAG_INFO|V8_CPPFLAGS: ${V8_CPPFLAGS}"
FLAG_INFO="$FLAG_INFO|V8_LDFLAGS:  ${V8_LDFLAGS}"
FLAG_INFO="$FLAG_INFO|V8_LIBS:     ${V8_LIBS}"
FLAG_INFO="$FLAG_INFO|."

dnl Local Variables:
dnl mode: outline-minor
dnl outline-regexp: "^\\(dnl --SECTION--\\)"
dnl End:
